package cn.wolfcode.web.controller;

import cn.wolfcode.domain.SystemDictionary;
import cn.wolfcode.domain.JsonResult;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.ISystemDictionaryService;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/systemDictionary")
public class SystemDictionaryController {
    @Autowired
    private ISystemDictionaryService systemDictionaryService;

    @RequestMapping("/list")
    @RequiresPermissions(value = {"systemDictionary:list","部门列表"},logical = Logical.OR)
    public String list(Model model, QueryObject qo){
        PageInfo<SystemDictionary> pageInfo = systemDictionaryService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        return "systemDictionary/list";
    }

    @RequestMapping("/delete")
    @ResponseBody
    @RequiresPermissions(value = {"systemDictionary:delete","部门删除"},logical = Logical.OR)
    public JsonResult delete(Long id){
        try {
            systemDictionaryService.delete(id);
            return new JsonResult();
        } catch(Exception e) {
            e.printStackTrace();
            return new JsonResult("操作失败",false);
        }

    }

    @RequestMapping("/input")
    @RequiresPermissions(value = {"systemDictionary:input","部门编辑"},logical = Logical.OR)
    public String input(Long id,Model model){
        if(id != null){
            SystemDictionary systemDictionary = systemDictionaryService.selectByPrimaryKey(id);
            model.addAttribute("systemDictionary", systemDictionary);
        }
        return "systemDictionary/input";
    }

    @RequestMapping("/saveOrUpdate")
    @RequiresPermissions(value = {"systemDictionary:saveOrUpdate","部门新增或修改"},logical = Logical.OR)
    @ResponseBody
    public JsonResult saveOrUpdate(Model model,@ModelAttribute("qo") SystemDictionary systemDictionary,Long id){
        try {
            if(id != null){
                systemDictionaryService.update(systemDictionary);
            }else {
                systemDictionaryService.insert(systemDictionary);
            }
            return new JsonResult();
        } catch(Exception e) {
            e.printStackTrace();
            return new JsonResult(e.getMessage(),false);
        }

    }
}
